package com.patent.service.impl;

import com.patent.common.CreateOrderNum;
import com.patent.common.PatentConstant;
import com.patent.common.ResultCode;
import com.patent.common.ResultData;
import com.patent.dao.ChangeRecordMapper;
import com.patent.domain.po.ChangeRecord;
import com.patent.domain.vo.ChangeInfoResponseVo;
import com.patent.service.ChangeRecordService;
import com.patent.service.InvoiceAmountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("changeRecordService")
@Transactional
public class ChangeRecordServiceImpl implements ChangeRecordService {

	private static final Logger log = LoggerFactory.getLogger(ChangeRecordServiceImpl.class);

	@Resource(name="changeRecordMapper")
	private ChangeRecordMapper  changeRecordMapper;

	@Resource(name="invoiceAmountService")
	private InvoiceAmountService invoiceAmountService;

	@Override
	public ResultData addChangeInfo(String userMobile, String patentId, String applicantsNew, String inventorsNew) {
		ResultData resultData = new ResultData();
		ChangeRecord changeRecord = new ChangeRecord();
		changeRecord.setPatentId(Integer.parseInt(patentId));
		changeRecord.setUserMobile(userMobile);
		changeRecord.setApplicantsNew(applicantsNew);
		changeRecord.setInventorsNew(inventorsNew);
		changeRecord.setCreateDate(new Date());
		changeRecord.setValidFlag(PatentConstant.YES_FLAG);
		changeRecord.setOrderState(PatentConstant.NO_PAY);
		//算价格
		changeRecord.setOfficialAmount(200d);
		changeRecord.setAgentAmount(200d);
		changeRecord.setOrderAmount(400d);
		changeRecord.setOrderNo(PatentConstant.CHANGE_ORDER_FLAG + CreateOrderNum.getOrderNo());
		changeRecordMapper.insertSelective(changeRecord);
		resultData.setCode(ResultCode.success.getCode());
		resultData.setMsg(ResultCode.success.getMessage());
		resultData.setResult(changeRecord.getOrderNo());
		return resultData;
	}

	@Override
	public ResultData queryChangeInfo(String userMobile, String queryFlag, Integer page, Integer rows) {
		ResultData resultData = new ResultData();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("userMobile",userMobile);
		map.put("page",(page-1)*rows);
		map.put("rows",rows);
		map.put("queryFlag",queryFlag);
		List<ChangeInfoResponseVo> list = changeRecordMapper.queryChangeInfo(map);
		resultData.setCode(ResultCode.success.getCode());
		resultData.setMsg(ResultCode.success.getMessage());
		resultData.setResult(list);
		return resultData;
	}

	@Override
	public ChangeRecord selectByPrimaryKey(Integer id) throws Exception {
		return changeRecordMapper.selectByPrimaryKey(id);
	}

	@Override
	public void updateByPrimaryKeySelective(ChangeRecord record) {
		changeRecordMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public ChangeRecord queryByOrderNo(String orderNo) {
		return changeRecordMapper.queryByOrderNo(orderNo);
	}

	@Override
	public void updateRecordC(ChangeRecord changeRecord) {
		try {
			updateByPrimaryKeySelective(changeRecord);
			invoiceAmountService.dealInvoiceAmount("add", changeRecord.getUserMobile(), changeRecord.getOrderAmount());
		} catch (Exception e) {
			e.printStackTrace();
			log.error("修改著录项变更失败：" + e.getMessage());
			throw new RuntimeException(e);
		}
	}
}
